//
// Created by 33145 on 2023/6/2.
//

#ifndef ROCKET_TCP_SERVER_H
#define ROCKET_TCP_SERVER_H

#include <set>
#include <memory>

#include "tcp_buffer.h"
#include "tcp_acceptor.h"
#include "tcp_connection.h"
#include "../net/io_thread_group.h"

namespace rocket {
  class TCPServer {
  public:

    typedef std::shared_ptr<TCPServer> ptr;

    explicit TCPServer(NetAddr::ptr local_addr);
    ~TCPServer() = default;

    void start();

  private:
    void init();
    void onAccept();

  private:
    TCPAcceptor::ptr _acceptor;
    NetAddr::ptr _local_addr;
    EventLoop::ptr _main_event_loop {nullptr}; // 主线程事件循环，只负责accept任务
    IOThreadGroup::ptr _io_thread_group {nullptr};  // 其他线程

    FdEvent::ptr _listen_fd_event;
    int _listen_client_counts {0};

    std::set<TCPConnection::ptr> _clients;  // TODO 现在还不能删除已经断开连接的连接，待解决
  };
}


#endif //ROCKET_TCP_SERVER_H
